package com.example.asus.mydiary;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.asus.Test.Diary;
import com.example.asus.db.domain.Password;
import com.example.asus.db.gen.NoteDao;
import com.example.asus.db.gen.PasswordDao;
import com.example.asus.db.service.GreenDaoManager;

import java.util.List;

public class SecondTime extends AppCompatActivity {
    private Password password;
    private PasswordDao passwordDao;
    private NoteDao noteDao;
    private String psw;
    private List<Password> getPsw;
    private EditText scetPsw;
    private Button scbtnCommit,scbtnForget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_time);

        initView();
        scbtnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkPsw();
            }
        });
        scbtnForget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog();
            }
        });
    }

    /**
     *  初始化控件
     */
    public void initView(){
        scetPsw = (EditText)findViewById(R.id.sc_psw);
        scbtnCommit = (Button)findViewById(R.id.sc_commit);
        scbtnForget = (Button)findViewById(R.id.sc_forget);
    }
    public void checkPsw(){
        // 获得对应数据的对象
        GreenDaoManager.InitDao(this,"password.db");
        passwordDao = GreenDaoManager.getDaoSession().getPasswordDao();
        getPsw = passwordDao.loadAll();
        System.out.print(getPsw.get(0));
        // 获取输入框值
        psw = scetPsw.getText().toString();
        if(psw.equals(getPsw.get(0).getPassword())){

            Intent intent = new Intent(SecondTime.this,DiaryRecycler.class);
            startActivity(intent);
            finish();
        }
        else{
            Toast.makeText(SecondTime.this,"密码错误，请重启输入！",Toast.LENGTH_LONG).show();
        }
    }

    /**
     *  忘记密码选择对话框
     */
    private void showNormalDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(SecondTime.this);
        normalDialog.setIcon(R.drawable.btn_warn);
        normalDialog.setTitle("重置密码");
        normalDialog.setMessage("你确定忘记密码！？\n重置密码将会删除所有日记！");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        GreenDaoManager.InitDao(SecondTime.this,"diary.db");
                        noteDao = GreenDaoManager.getDaoSession().getNoteDao();
                        noteDao.deleteAll();
                        GreenDaoManager.InitDao(SecondTime.this,"password.db");
                        passwordDao = GreenDaoManager.getDaoSession().getPasswordDao();
                        passwordDao.deleteAll();

                        // 跳转回第一次登陆的页面设置密码
                        Intent intent = new Intent(SecondTime.this,FirstTime.class);
                        startActivity(intent);
                        finish();
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }
}
